package com.newdjk.newdoctor.ui;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.newdjk.newdoctor.BasActivity;
import com.newdjk.newdoctor.R;
import com.newdjk.newdoctor.entity.AppEntity;
import com.newdjk.newdoctor.entity.ServiceIndrocutionEntity;
import com.newdjk.newdoctor.net.BaseComPleteObserver;
import com.newdjk.newdoctor.net.NetServices;
import com.newdjk.newdoctor.service.MyService;
import com.newdjk.newdoctor.utils.AppUpdateUtils;
import com.newdjk.newdoctor.utils.AppUtils;
import com.newdjk.newdoctor.utils.HomeUtils;
import com.newdjk.newdoctor.utils.PDFviewUtils;
import com.newdjk.newdoctor.utils.ToastUtil;
import com.newdjk.okhttp.entity.AboutUsEntity;
import com.newdjk.okhttp.entity.BaseEntity;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 重置密码
 */
public class AboutUsActivity extends BasActivity {


    private static final String TAG = "AboutUsActivity";
    @BindView(R.id.tv_kefu)
    TextView tvKefu;
    @BindView(R.id.tv_email)
    TextView tvEmail;
    @BindView(R.id.mVersion)
    TextView mVersion;
    @BindView(R.id.mBundleID)
    TextView mBundleID;
    @BindView(R.id.tv_update_desc)
    TextView tvUpdateDesc;
    @BindView(R.id.tv_update)
    TextView tvUpdate;
    @BindView(R.id.lv_update)
    LinearLayout lvUpdate;
    @BindView(R.id.tv_privacy)
    TextView tvPrivacy;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    private int mMustupdate = 0;
    private String mApkUrl = "";

    @Override
    protected String setTitle() {
        return "关于我们";
    }

    @Override
    protected int initViewResId() {
        return R.layout.activity_about_us;
    }


    @Override
    protected void initListener() {
        tvUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShowUpdate();
            }
        });

        tvPrivacy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent Privacy = new Intent(AboutUsActivity.this, PrivacyActivity.class);
                startActivity(Privacy);
            }
        });

        tvAgreement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AboutUsActivity.this, AgreementActivity.class);
                startActivity(intent);
            }
        });

        mVersion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                try {
//                    Gson mGson = new Gson();
//                    String data = "{\"title\":\"检验检查\",\"linkUrl\":\"http://resource.newdjk.cn/ServicePackPay/20201102/8f7a89c6-984e-4956-b1ee-9f792fd14e76.pdf\"}";
//                    ServiceIndrocutionEntity entity = mGson.fromJson(data, ServiceIndrocutionEntity.class);
//                    PDFviewUtils.getInstanse().showPDF(AboutUsActivity.this, entity.getLinkUrl(), entity.getTitle());
//                } catch (Exception e) {
//                    ToastUtil.setToast("数据解析失败，无法查阅报告");
//                }
            }
        });
    }

    private void ShowUpdate() {
        HomeUtils.INSTANCE.checkVersion(new HomeUtils.UpdateListener() {
            @Override
            public void success(final AppEntity entity) {
                if (MyService.isDownload) {
                    ToastUtil.setToast("app正在升级中，请稍后");
                    return;
                }
                AppUpdateUtils.getInstance().update(entity, AboutUsActivity.this, AboutUsActivity.this);

            }


            @Override
            public void failed(int statusCode, String errorMsg) {
                ToastUtil.setToast(errorMsg);

            }
        });
    }

    @Override
    protected void initData() {
        getLocalVersion(this);
        getAboutInfo();
        checkUpdate();
    }

    private void checkUpdate() {
        HomeUtils.INSTANCE.checkVersion(new HomeUtils.UpdateListener() {
            @Override
            public void success(final AppEntity entity) {
                final String apkUrl = entity.getAppPath();
                int apkCode = entity.getAppVersion();
                int versionCode = AppUtils.getAppVersionCode(AboutUsActivity.this);
                mMustupdate = entity.getMustUpdate();
                Log.d(TAG, apkCode + "  " + versionCode);
                mApkUrl = apkUrl;
                if (apkCode > versionCode) {
                    tvUpdateDesc.setText("检测到当前最新版本为" + entity.getReleaseVersion());
                    tvUpdate.setText("立即升级");
                    tvUpdate.setVisibility(View.VISIBLE);
                } else {
                    tvUpdateDesc.setText("当前版本已经是最新版本");
                    tvUpdate.setVisibility(View.GONE);
                }

            }


            @Override
            public void failed(int statusCode, String errorMsg) {


            }
        });

    }

    public void getLocalVersion(Context context) {
        String localVersion;
        try {
            PackageInfo packageInfo = context.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(context.getPackageName(), 0);
            localVersion = packageInfo.versionName;
            int versionCode = AppUtils.getAppVersionCode(this);
            mVersion.setText("当前版本号：V" + localVersion);
            mBundleID.setText("BundleID: " + versionCode);
            Log.d("TAG", "本软件的版本号。。" + localVersion);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initView() {

    }

    @Override
    public boolean IsShowBackIcon() {
        return true;
    }

    /**
     * 完成
     */

    public void getAboutInfo() {

        NetServices.Factory.getService().GetAboutInfo(
        ).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseComPleteObserver<AboutUsEntity>(AboutUsActivity.this) {
                    @Override
                    protected void onSuccees(BaseEntity<AboutUsEntity> t) throws Exception {
                        tvKefu.setText("客服热线：" + t.getData().getMobile());
                        tvEmail.setText("客服邮箱：" + t.getData().getMail());

                    }
                });


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
